﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMS.DAO;
using log4net;
using MMS.Validation;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors;

using DevExpress.XtraEditors.Controls;

namespace MMS
{
    public partial class FormUnitInfo : Form
    {
        private UnitDTO unitDTO;
        private bool UIHasError = false;
        public FormUnitInfo()
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            unitDTO = new UnitDTO();
            unitDTO.Id = "";

        }

        public FormUnitInfo(UnitDTO unitDTO)
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            try
            {
                this.unitDTO = unitDTO;
                populateUnitInfo();
            }
            catch (Exception e)
            {
                LogUtil.log.Error(e);
            }

        }

        private void populateUnitInfo()
        {
            txtUnitId.Text = unitDTO.Id.ToString();
            // txtUnitName.Text = unitDTO.UnitName;
            txtUnitName.SelectedText = unitDTO.UnitName;
            txtUnitNote.Text = unitDTO.Note;
        }

        private void saveUnit()
        {
            //unit.Id = Int32.Parse(txtUnitId.Text);
            unitDTO.UnitName = txtUnitName.Text;
            unitDTO.Note = txtUnitNote.Text;

            UnitDAO unitDAO = new UnitDAO(Param.ConnectionString.Value);
            if (unitDTO.Id == "")
            {

                unitDAO.Insert(unitDTO);

            }

            else
            {
                unitDAO.Update(unitDTO);
            }

        }



        private void btnProcess_Click(object sender, EventArgs e)
        {

            if (((SimpleButton)sender).Name.Equals(btnSave.Name))
            {
                if (!UIHasError && !isEmpty())
                {
                    saveUnit();
                    this.Close();
                }

                else
                {
                    lbErrorString.ForeColor = Color.Red;
                    lbErrorString.Text = MessageBundle.ERROR_UI.Value;
                    Utils.startTimer(timer1);
                }
            }
            else if (((SimpleButton)sender).Name.Equals(btnExit.Name))
            {

                this.Close();
            }
        }

        private bool isEmpty()
        {
            bool result = false;
           if (txtUnitName.Text.Equals(""))
           {
               result= true;
           }
           return result;

        }

        private void FormUnitInfo_Load(object sender, EventArgs e)
        {
            bindUIToDevExValidation();
        }
        private void bindUIToDevExValidation()
        {
            // Specify the type of records stored in the BindingSource.
            bindingSource1.DataSource = typeof(UnitDTO);
            bindingSource1.Add(unitDTO);
            // Bind the text editors to the MyRecord.FirstName and MyRecord.LastName properties.
            txtUnitName.DataBindings.Add(new Binding("EditValue", this.bindingSource1,
              "UnitName", true));


            // Bind the DXErrorProvider to the data source.
            dxErrorProvider1.DataSource = bindingSource1;
            // Specify the container of controls (textEdit1 and textEdit2) 
            // which are monitored for errors.
            dxErrorProvider1.ContainerControl = this;

            // DXErrorProvider.GetErrorIcon += new GetErrorIconEventHandler(DXErrorProvider_GetErrorIcon);

            // Associate an error of the User1 error type with a txtUnitName control.
            // dxErrorProvider1.SetError(txtUnitName, "Lỗi", ErrorType.Default);
        }

        // Provide an error icon for the User1 error type.
        void DXErrorProvider_GetErrorIcon(GetErrorIconEventArgs e)
        {

        }


        private void txtUnitName_TextChanged(object sender, EventArgs e)
        {
            /*
            if (txtUnitName.Text == "")
            {
                dxErrorProvider1.SetError(txtUnitName,MessageBundle.ERROR_EMPTY_FIELD.Value, ErrorType.Default);
            }
            else
            {
                dxErrorProvider1.ClearErrors();
            }
            if (dxErrorProvider1.HasErrors)
            {
                btnSave.Enabled = false;
            }
            else
            {
                btnSave.Enabled = true;
            }*/

        }



        private void txtUnitName_Validating(object sender, CancelEventArgs e)
        {
            if (txtUnitName.Text == "")
            {
                UIHasError = true;
            }
            else
            {
                UIHasError = false;
            }


        }

        private void txtUnitName_InvalidValue(object sender, InvalidValueExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            dxErrorProvider1.SetError(txtUnitName, MessageBundle.ERROR_EMPTY_FIELD.Value, ErrorType.Default);

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            lbErrorString.Text = "";
        }

    }
}

